package cn.icanci.loopstack.bic.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.biz.mapper.EdgeMapper;
import cn.icanci.loopstack.bic.biz.service.EdgeService;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.EdgeVO;
import cn.icanci.loopstack.bic.common.model.base.NodeVO;
import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.EdgeDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.EdgeDO;

import com.google.common.collect.Lists;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/16 09:55
 */
@Service
public class EdgeServiceImpl implements EdgeService {
    @Resource
    private EdgeDAO    edgeDAO;
    @Resource
    private EdgeMapper edgeMapper;

    @Override
    public List<EdgeVO> queryAll() {
        return edgeMapper.dos2vos(edgeDAO.queryAll());
    }

    @Override
    public void save(EdgeVO edgeVO) {
        // TODO 日志记录
        // TODO 事务插入 测试
        if (doInsert(edgeVO)) {
            EdgeDO insert = edgeMapper.vo2do(edgeVO);
            edgeDAO.insert(insert);
            // TODO
            // 插入样式
            // 插入组件函数

        } else {
            edgeDAO.update(edgeMapper.vo2do(edgeVO));
        }
    }

    @Override
    public EdgeVO queryById(String id) {
        return edgeMapper.do2vo(edgeDAO.queryOneById(id));
    }

    @Override
    public PageList<EdgeVO> queryPage(EdgeVO edgeVO, int pageNum, int pageSize) {
        PageList<EdgeDO> pageQuery = edgeDAO.pageQuery(edgeMapper.vo2do(edgeVO), pageNum, pageSize);
        return new PageList<>(edgeMapper.dos2vos(pageQuery.getData()), pageQuery.getPaginator());

    }

    @Override
    public EdgeVO queryBySystemUuidAndName(String systemUuid, String name) {
        return edgeMapper.do2vo(edgeDAO.queryBySystemUuidAndName(systemUuid, name));
    }

    @Override
    public EdgeVO queryBySystemUuidAndKey(String systemUuid, String key) {
        return edgeMapper.do2vo(edgeDAO.queryBySystemUuidAndKey(systemUuid, key));
    }

    @Override
    public List<TextValue> loadSelector(String systemUuid) {
        List<EdgeVO> edges = queryAll();
        List<TextValue> textValues = Lists.newArrayList();
        for (EdgeVO edge : edges) {
            if (!StringUtils.equals(systemUuid, edge.getSystemUuid())) {
                continue;
            }

            String label;
            if (isDeleted(edge)) {
                label = String.format(DELETED_FORMAT, edge.getEdgeName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, edge.getEdgeName());
            }
            String value = edge.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    @Override
    public List<TextValue> loadAllSelector() {
        List<EdgeVO> edges = queryAll();
        List<TextValue> textValues = Lists.newArrayList();
        for (EdgeVO edge : edges) {
            String label;
            if (isDeleted(edge)) {
                label = String.format(DELETED_FORMAT, edge.getEdgeName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, edge.getEdgeName());
            }
            String value = edge.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }
}
